package com.cloudptt.wp.ui.message.adapter;

import java.util.List;

import com.cloudptt.api.comm.Common;
import com.cloudptt.api.comm.FileOperation;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mcptt.config.body.info.mcdata.FileMataInfo;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.HistoryInfo;
import com.mcptt.db.model.SessionInfo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class HistoryAdapter extends BaseAdapter {

	private static final String THIS_FILE = "HistoryAdapter";
	private Context ctx = null;
	private List<HistoryInfo> list = null;
	private AddrInfo curGroupInfo = null;
	
	public HistoryAdapter(Context ctx,List<HistoryInfo> list,AddrInfo curGroupInfo)
	{
		Log.i(THIS_FILE,"new curGroupInfo =  "+ curGroupInfo);
		this.ctx = ctx;
		this.list = list;
		this.curGroupInfo = curGroupInfo;
	}
	
	public void updateData(List<HistoryInfo> list,AddrInfo curGroupInfo){
		Log.i(THIS_FILE,"updateData curGroupInfo =  "+ curGroupInfo);
		this.list = list;
		this.curGroupInfo = curGroupInfo;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list != null){
			return list.size();
		}else{
			return 0;
		}
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.home_history_list_item, null);

			viewHolder = new ViewHolder();
			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.defaultTv = (TextView)convertView.findViewById(R.id.defaultTv);
//			viewHolder.curTv = (TextView)convertView.findViewById(R.id.curTv);
			viewHolder.date = (TextView) convertView.findViewById(R.id.date);
			viewHolder.content = (TextView) convertView.findViewById(R.id.contentmsg);
			viewHolder.count = (TextView) convertView.findViewById(R.id.count_tv);
			viewHolder.pic       = (ImageView)convertView.findViewById(R.id.qcb);

			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		HistoryInfo history = list.get(position);
		Log.i(THIS_FILE,"getView history =  "+ history);
		
		AddrInfo addrInfo = history.getPeerAddrInfo();
		if(addrInfo.isGroupType())
		{
			viewHolder.pic.setImageResource(R.drawable.group_icon_background);
			
		}else if(addrInfo.isSys()
				|| addrInfo.isDac())
		{
			viewHolder.pic.setImageResource(R.drawable.dac_icon_background_online);	
			
		}else{
			viewHolder.pic.setImageResource(R.drawable.user_icon_background_online);
		}
		
		if(addrInfo.isGroupType())
		{
			if(addrInfo.getAttr() == 1){
				viewHolder.defaultTv.setText(R.string.name_default);
				viewHolder.defaultTv.setVisibility(View.VISIBLE);
			}else{
				viewHolder.defaultTv.setText("");
				viewHolder.defaultTv.setVisibility(View.GONE);
			}
			Log.i(THIS_FILE,"getView curGroupInfo =  "+ curGroupInfo + " addrInfo = " + addrInfo);
//			if(addrInfo.isSameOne(curGroupInfo)){
//				viewHolder.curTv.setText(R.string.name_cur);
//				viewHolder.curTv.setVisibility(View.VISIBLE);
//			}else{
//				viewHolder.curTv.setText("");
//				viewHolder.curTv.setVisibility(View.GONE);
//			}
		}else{
			viewHolder.defaultTv.setText("");
			viewHolder.defaultTv.setVisibility(View.GONE);
//			viewHolder.curTv.setText("");
//			viewHolder.curTv.setVisibility(View.GONE);
		}
		
		String dscp = history.getDscp();
		viewHolder.name.setText(dscp);
		String content = history.getLastMsgContent();
		
		if(content != null
			&& content.isEmpty()){
			viewHolder.content.setVisibility(View.GONE);
		}
		else
			{
			int msgType = history.getLastContentType();
			if(msgType == SessionInfo.ContentType_FileURL)
			{
				viewHolder.content.setVisibility(View.VISIBLE);
				Log.i(THIS_FILE," content is  "+ content);
				FileMataInfo fileMataInfo = FileMataInfo.parse(content);
				String name = fileMataInfo.getFileName();
				String type = fileMataInfo.getType();
				Log.debug(getClass(), "getView name = " + name + " type = " + type);

				if(FileOperation.isPicture(type))
				{
					viewHolder.content.setText(R.string.pic_flag);
				}
				else if(FileOperation.isAudio(type))
				{
					viewHolder.content.setText(R.string.voice_flag);
				}
				else
				{
					viewHolder.content.setText(R.string.file_flag);
				}
//				viewHolder.content.setText(R.string.file_flag);
			}
//			else if(msgType == SessionInfo.Msgtype_Pic)
//			{
//				viewHolder.content.setVisibility(View.VISIBLE);
//				viewHolder.content.setText(R.string.pic_flag);
//			}
			else
			{
				viewHolder.content.setVisibility(View.VISIBLE);
				Log.debug(getClass(), "getView content = " + content);
				viewHolder.content.setText(content);
//				if(content != null
//						&& content.length() > 0) {
//					try {
//						JsonParser jsonParser = new JsonParser();
//						JsonObject obj = jsonParser.parse(content).getAsJsonObject();
//						Log.debug(getClass(), "getView obj = " + obj);
//
//						if(obj != null
//							&& obj.get("SDS") != null) {
//							String str = obj.get("SDS").getAsString();
//							viewHolder.content.setText(str);
//						}
//					}
//					catch (Exception e)
//					{
//						e.printStackTrace();
//						viewHolder.content.setText("");
//					}
//				}
//				else
//				{
//					viewHolder.content.setText("");
//				}
			}
		}
		int contentType = history.getLastContentType();
//		viewHolder.date.setText(date.substring(5,date.length()-3));

		int count = history.getUnreadCount();
		if (count == 0) {
			viewHolder.count.setVisibility(View.GONE);

		} else {
			viewHolder.count.setText(count+"");
			viewHolder.count.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	private class ViewHolder {
		public ImageView pic;
		public TextView name;
		public TextView defaultTv;
//		public TextView curTv;
		public TextView date;
		public TextView content;
		public TextView count;
	}
}
